﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using DualControl.ControlMeasures;
using DualControl.ControlMeasures.Dtos;
using DualControl.Departments;
using DualControl.HazardSources;
using DualControl.RiskLibrary.Dtos;
using DualControl.RiskPoints;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

using System.Threading.Tasks;

using System.Linq;
using Abp.AutoMapper;

using Abp.Linq.Extensions;

using System.Linq.Dynamic.Core;



namespace DualControl.RiskLibrary
{
    public class RiskLibraryAppService: DualControlAppServiceBase,IRiskLibraryAppService
    {
        private readonly IRepository<ControlMeasure, int> _controlmeasureRepository;

        private readonly IRepository<HazardSource> _hazardsourceRepository;

        private readonly IRepository<RiskPoint> _riskpointrepository;

        private readonly IRepository<Department, long> _organizationUnitRepository;

        public RiskLibraryAppService(
        IRepository<ControlMeasure, int> controlmeasureRepository,
        IRepository<HazardSource> hazardsourceRepository,
       IRepository<RiskPoint> riskpointrepository,
       IRepository<Department, long> organizationUnitRepository
       
            )
        {
            _controlmeasureRepository = controlmeasureRepository;
            _hazardsourceRepository = hazardsourceRepository;
            _riskpointrepository = riskpointrepository;
            _organizationUnitRepository = organizationUnitRepository;           
        }


        public async Task<PagedResultDto<HSNotificationListDto>> GetPagedHSControlMeasure(GetHSNotificationInput input)
        {
            var query = from hs in _hazardsourceRepository.GetAll().Include("ControlMeasures").Include("EmergencyMeasures")
                        join rp in _riskpointrepository.GetAll() on hs.RiskPointId equals rp.Id
                        select new HSNotificationListDto
                        {
                            Id = hs.Id,
                            //风险点
                            Code = rp.Code,
                            RiskPointId = rp.Id,
                            OrganizationUnitId = rp.OrganizationUnitId,
                            RiskPointName = rp.Name,
                            HazardSourceName = hs.HazardSourceName,
                            RiskFactor = hs.RiskFactor,
                            RiskLevel = hs.RiskLevel,
                            AccidentTypes = hs.AccidentTypes,
                            ControlMeasures = hs.ControlMeasures,
                            EmergencyMeasures = hs.EmergencyMeasures
                        };


            var hazardsourceCount = await query.CountAsync();

            var hazardsources = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();
            var hazardsourceListDtos = hazardsources.MapTo<List<HSNotificationListDto>>();
            return new PagedResultDto<HSNotificationListDto>(
                 hazardsourceCount, hazardsourceListDtos
                 );
        }
    }
}
